/**
 *
 * @author Tritonio
 */

package jrpc;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

class Connection {

    Socket sock;
    Cipher inputCipher, outputCipher;
    CipherInputStream input;
    CipherOutputStream output;

    Connection(RemoteHost host, char[] password) throws IOException, UnknownHostException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
        sock = new Socket(host.hostname, host.port);
        sock.setSoTimeout(10000); //Maybe less?
        inputCipher = Cipher.getInstance("Blowfish/CFB/ISO10126Padding");
        outputCipher = Cipher.getInstance("Blowfish/CFB/ISO10126Padding");
        int i;
        byte[] rawpass = null;
        for (i=password.length; i<=16; i++) password[i]='@';
        for (i=0; i<=16; i++) rawpass[i]=(byte) password[i];
        Key mykey = new SecretKeySpec(rawpass, "Blowfish");
        outputCipher.init(Cipher.ENCRYPT_MODE, mykey);
        inputCipher.init(Cipher.DECRYPT_MODE, mykey);
        input = new CipherInputStream(sock.getInputStream(),inputCipher);
        output = new CipherOutputStream(sock.getOutputStream(),outputCipher);
    }

}
